close all
clear all

readmodel;

%% Shocks
listshocks = {'shock_def_rat','shock_prem','shock_tprem10','shock_g_y_tnd'};
listtitles = {'1pp Positive Shock to Total Deficit Ratio','1pp Positive Shock to Country Risk Premium','1pp Positive Shock to 10Y Term Premium', ... 
				'1pp Negative Shock to Potential Output Growth' };

% Sets the time frame for the simulation 
startsim = yy(2011);
endsim = yy(2021);

% For each shock a zero database is created (command 'zerodb') and named as 
% database 'd.{shock_name}'
for i = 1:length(listshocks);
    d.(listshocks{i}) = sstatedb(m,startsim:endsim);
end

% Fills the respective databases with the shocks' values for the starting
% point of the simulation (startsim). For simplicity, all shocks are set to
% 1 percent
d.shock_def_rat.shock_def_rat(startsim) =  1;
d.shock_prem.shock_prem(startsim)       =  1;
d.shock_tprem10.shock_tprem10(startsim) =  1;
d.shock_g_y_tnd.shock_g_y_tnd(startsim) = -1;

% Simulates the shocks using the command 'simulate'. For this model
% 'm' and respective database 'd.{shock_name} are used. Results are written 
% in object 's.{shock_name}'. Command 'dbextend' merges the historical 
% database d.{shock_name} with simulation database s.{shock_name}. 
for i=1:length(listshocks);    
    s.(listshocks{i}) = simulate(m,d.(listshocks{i}),startsim:endsim,'deviation',false);
    s.(listshocks{i}) = dbextend(d.(listshocks{i}),s.(listshocks{i}));
end

% Report
x = report.new('Shocks');

% Figures
sty = struct();
sty.line.linewidth = 1.5;
sty.line.linestyle = {'-';'--'};
sty.line.color = {'k';'r'};
sty.axes.box = 'on';
sty.legend.location = 'SouthOutside';

rng = startsim-1:endsim;
dTick = rng(1):3:rng(end);

for i = 1:length(listshocks);
x.figure(listtitles{i},'subplot',[3,3],'style',sty,'range',rng,...
  'dateformat','YYYY:P','DateTick',dTick);

x.graph(s.(listshocks{i}).def_rat.comment,'legend',false);
x.series('',[s.(listshocks{i}).def_rat]);

x.graph(s.(listshocks{i}).primdef_rat.comment,'legend',false);
x.series('',[s.(listshocks{i}).primdef_rat]);

x.graph(s.(listshocks{i}).def_struct_rat.comment,'legend',false);
x.series('',[s.(listshocks{i}).def_struct_rat]);

x.graph(s.(listshocks{i}).fiscimp_rat.comment,'legend',false);
x.series('',[s.(listshocks{i}).fiscimp_rat]);

x.graph(s.(listshocks{i}).dl_y.comment,'legend',false);
x.series('',[s.(listshocks{i}).dl_y]);

x.graph(s.(listshocks{i}).dl_cpi.comment,'legend',false);
x.series('',[s.(listshocks{i}).dl_cpi]);

x.graph(s.(listshocks{i}).rsg1.comment,'legend',false);
x.series('',[s.(listshocks{i}).rsg1]);

x.graph(s.(listshocks{i}).dl_czk_eur.comment,'legend',false);
x.series('',[s.(listshocks{i}).dl_czk_eur]);

x.graph(s.(listshocks{i}).prem.comment,'legend',false);
x.series('',[s.(listshocks{i}).prem]);

x.pagebreak();

x.figure(listtitles{i},'subplot',[3,3],'style',sty,'range',rng,...
  'dateformat','YYYY:P','DateTick',dTick);

x.graph(s.(listshocks{i}).debt_rat.comment,'legend',false);
x.series('',[s.(listshocks{i}).debt_rat]);

x.graph(s.(listshocks{i}).rsg3.comment,'legend',false);
x.series('',[s.(listshocks{i}).rsg3]);

x.graph(s.(listshocks{i}).rsg10.comment,'legend',false);
x.series('',[s.(listshocks{i}).rsg10]);

x.graph(s.(listshocks{i}).rsg_fcy.comment,'legend',false);
x.series('',[s.(listshocks{i}).rsg_fcy]);

x.graph(s.(listshocks{i}).tprem3.comment,'legend',false);
x.series('',[s.(listshocks{i}).tprem3]);

x.graph(s.(listshocks{i}).tprem10.comment,'legend',false);
x.series('',[s.(listshocks{i}).tprem10]);

x.graph(s.(listshocks{i}).lrr_tnd.comment,'legend',false);
x.series('',[s.(listshocks{i}).lrr_tnd]);

x.graph([s.(listshocks{i}).l_y_tnd.comment 'y-o-y'],'legend',false);
x.series('',[s.(listshocks{i}).l_y_tnd - s.(listshocks{i}).l_y_tnd{-1}]);

x.graph([s.(listshocks{i}).l_z_tnd.comment 'y-o-y'],'legend',false);
x.series('',[s.(listshocks{i}).l_z_tnd - s.(listshocks{i}).l_z_tnd{-1}]);

end

x.publish('Report_Shocks','display',false);